<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item
        label="充值人"
        prop="userName"
      >
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入充值人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="是否代充"
        prop="isBehalf"
      >
        <el-select
          v-model="queryParams.isBehalf"
          placeholder="请选择是否代充"
          clearable
          size="small"
        >
          <el-option
            @keyup.enter.native="handleQuery"
            v-for="o, i in [{ label: '否', isBehalf: '0' }, { label: '是', isBehalf: '1' }]"
            :key="i"
            :label="o.label"
            :value="o.isBehalf"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item-->
<!--        label="代充值人"-->
<!--        prop="behalfUserName"-->
<!--      >-->
<!--        <el-input-->
<!--          v-model="queryParams.behalfUserName"-->
<!--          placeholder="请输入代充值人"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item
        label="充值金额"
        prop="amount"
      >
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入充值金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="审核状态"
        prop="status"
      >
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
          size="small"
        >
          <el-option
            v-for="o, i in [{ label: '未审核', status: '0' }, { label: '未通过', status: '1' }, { label: '通过', status: '2' }]"
            :key="i"
            :label="o.label"
            :value="o.status"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        label="部门"
        prop="department"
      >
        <template>
          <el-select
            v-model="queryParams.department"
            collapse-tags
            style="margin-left: 20px;width:300px"
            placeholder="请选择部门"
          >
            <el-option
              v-for="item in options"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </template>
      </el-form-item>

      <el-form-item
        label="充值时间"
        prop="rechargeTime"
      >
        <el-date-picker
          clearable
          size="small"
          style="width: 300px"
          v-model="dateRange"
          :default-time="['00:00:00', '23:59:59']"
          @change="handleQuery"
          type="daterange"

          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
      <div  STYLE="text-align: right;
               margin-top: -1px;
               margin-right: 110px;">
        <div> 总金额:{{ sumAmount }} 元</div>
      </div>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col
        :span="1.5"
        v-if="false"
      >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:record:add']"
        >新增</el-button>
      </el-col>
      <el-col
        :span="1.5"
        v-if="false"
      >
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col
        :span="1.5"
        v-if="false"
      >
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:record:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="recordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      />
      <el-table-column
        label="序号"
        align="center"
        prop="id"
      > <template slot-scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column
        label="部门"
        align="center"
        prop="deptName"
      />
      <el-table-column
        label="充值人"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="充值人手机号"
        align="center"
        prop="telephone"
      />
      <el-table-column
        label="是否代充"
        align="center"
        prop="isBehalf"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.isBehalf == '0'">否</span>
          <span v-if="scope.row.isBehalf == '1'">是</span>
        </template>
      </el-table-column>
      <el-table-column
        label="代充值人"
        align="center"
        prop="behalfUserName"
      />
      <el-table-column
        label="代充值人手机号"
        align="center"
        prop="behalfTelephone"
      />
      <el-table-column
        label="金额"
        align="center"
        width="100"
        prop="amount"
      />
      <el-table-column
        label="充值时间"
        align="center"
        prop="rechargeTime"
        width="180"
      />
      <el-table-column
        label="余额"
        align="center"
        prop="balance"
        width="180"
      >
        <!-- <template slot-scope="scope"> <span>{{ parseTime(scope.row.rechargeTime, '{y}-{m}-{d}') }}</span></template> -->
      </el-table-column>
      <el-table-column
        label="审核状态"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">未审核</span>
          <span v-if="scope.row.status == '1'">未通过</span>
          <span v-if="scope.row.status == '2'">通过</span>
        </template>
      </el-table-column>/>
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <!-- <template slot-scope="scope"> <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span></template> -->
      </el-table-column>
      <el-table-column
        label="审核人名称"
        align="center"
        prop="auditUserName"
      />
      <el-table-column
        label="审核人手机号"
        align="center"
        prop="auditTelephone"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-add-location"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:record:edit']"
            v-if="scope.row.status == 0"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete-location"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:record:edit']"
            :disabled="true"
            v-else
          >已审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="false"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <!-- <el-form-item label="充值人" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入充值人" />
                </el-form-item>
                <el-form-item label="是否代充" prop="isBehalf">
                    <el-input v-model="form.isBehalf" placeholder="请输入是否代充" />
                </el-form-item>
                <el-form-item label="代充值人  " prop="behalfUserId">
                    <el-input v-model="form.behalfUserId" placeholder="请输入代充值人" />
                </el-form-item>
                <el-form-item label="消费金额" prop="amount">
                    <el-input v-model="form.amount" placeholder="请输入消费金额" />
                </el-form-item>
                <el-form-item label="充值时间" prop="rechargeTime">
                    <el-date-picker clearable size="small" style="width: 200px" v-model="form.rechargeTime" type="date"
                        value-format="yyyy-MM-dd" placeholder="选择充值时间">
                    </el-date-picker>
                </el-form-item> -->
        <el-form-item label="审核状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">未通过</el-radio>
            <el-radio label="2">通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="审核时间" prop="auditTime">
                    <el-date-picker clearable size="small" style="width: 200px" v-model="form.auditTime" type="date"
                        value-format="yyyy-MM-dd" placeholder="选择审核时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="审核人id" prop="operator">
                    <el-input v-model="form.operator" placeholder="请输入审核人id" />
                </el-form-item> -->
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
  exportRecord,
} from "@/api/system/record";

import { listDept } from "@/api/system/dept";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,

      sumAmount:0,
      // 【请填写功能名称】表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        isBehalf: undefined,
        behalfUserId: undefined,
        amount: undefined,
        rechargeTime: undefined,
        status: undefined,
        auditTime: undefined,
        operator: undefined,
        beginTime: undefined,
        endTime: undefined,
      department:undefined,
      },
      // 表单参数
      form: {},
      options:[],
      // 表单校验
      rules: {},
      dateRange: [],
      //时间段，不能选择之前时间
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 8.64e6;
        },
      }
    };
  },
  created() {
    this.getList();
    this.getSecondDeptlist();
  },
  methods: {
    /** 获取二级部门列表 */
    getSecondDeptlist() {
      this.loading = true;
      listDept().then((response) => {
        console.log(`getSecondDeptlist:`, response.data);
        this.options = response.data;
      });
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = undefined;
        this.queryParams.endTime = undefined;
      }
      listRecord(this.queryParams).then((response) => {
        this.recordList = response.rows;
        this.total = response.total;

        this.sumAmount = response.sumAmount
        this.loading = false;
      });
    },
    //  搜索时间段
    queryDatePiacker(date) {
      console.log(`date:`, date);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        isBehalf: undefined,
        behalfUserId: undefined,
        amount: undefined,
        rechargeTime: undefined,
        status: "0",
        auditTime: undefined,
        operator: undefined,
        createTime: undefined,
        updateTime: undefined,
        department:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(`111111`);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "审核充值";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRecord(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "审核充值结果";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(`this.form:`, this.form);
          if (this.form.id != undefined) {
            updateRecord(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRecord(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出充值报表?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportRecord(queryParams);
        })
        .then((response) => {
          this.download(response.data);
        })
        .catch(function () {});
    },
  },
};
</script>
